package com.ask.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.ask.common.Result;
import com.ask.order.bean.Order;
import com.ask.order.properties.OrderProperties;
import com.ask.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@RefreshScope
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private OrderProperties orderProperties;

    @GetMapping("/config")
    public String config() {
        return orderProperties.getTimeOut() + "--" + orderProperties.getAutoConfirm() + "--"
                + orderProperties.getDbUrl();
    }

    @GetMapping("/create")
    public Order create(@RequestParam("productId") Long productId,
                        @RequestParam("userId") Long userId) {
        Order order = orderService.createOrder(productId, userId);
        return order;
    }

    @GetMapping("/seckill")
    @SentinelResource(value = "seckill-order", fallback = "seckillFallBack")
    public Order seckill(@RequestParam("productId") Long productId,
                         @RequestParam("userId") Long userId) {
        Order order = orderService.createOrder(productId, userId);
        order.setId(Long.MAX_VALUE);
        return order;
    }


    @SentinelResource(value = "seckill-order", fallback = "seckillFallBack")
    public Order seckillFallBack(Long productId, Long userId) {
        Order order = new Order();
        order.setId(0L);
        order.setProductId(productId);
        order.setUserId(userId);
        order.setInfo("seckillFallBack兜底回调触发");
        return order;
    }

    @GetMapping("/readDb")
    public Result readDb(){
        System.out.println("read db...");
        return Result.success("read db success");
    }


}
